﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HungryCrowd.category
{
    public partial class update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var cat = new HungryCrowdLibrary.ModelDto.Category();
            cat.Name = txtName.Text;
            cat.Synopsis = txtSynopsis.Text;
            cat.ImageUrl = SaveImage().Replace('\\','/');
            cat.IsActive = true;
            hdfHiddenField.Value = new JavaScriptSerializer().Serialize(cat);
        }

        private string SaveImage()
        {
            var path = string.Empty;
            var imageURL = string.Empty;

            if (fluImage.HasFile)
            {
                path = ConfigurationManager.AppSettings["filePath"];
                if (Directory.Exists(path))
                {
                    path = string.Format("{0}{1}", ConfigurationManager.AppSettings["filePath"], fluImage.FileName);
                    File.WriteAllBytes(path, fluImage.FileBytes);
                    imageURL = string.Format("{0}{1}",ConfigurationManager.AppSettings["imageURL"], path.Substring(path.IndexOf("images"), path.Length - path.IndexOf("images")));
                }

            }
            return string.IsNullOrWhiteSpace(imageURL) ? "no file saved" : imageURL;
        }
    }
}